﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Helpers
{
	using System.Globalization;
	using System.IO;
	using System.Text;
	using System.Web.Routing;
	using System.Web.UI;
	using Falcon.Core.Extensions;
	using Falcon.Core.Menu;

	public class MenuHelper
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="MenuHelper"/> class.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="builder">The builder.</param>
		public MenuHelper(RequestContext context, MenuItemBuilder builder)
		{
			this.Context = context;
			this.Builder = builder;
		}

		/// <summary>
		/// Gets or sets the context.
		/// </summary>
		/// <value>The context.</value>
		public RequestContext Context
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the builder.
		/// </summary>
		/// <value>The builder.</value>
		public MenuItemBuilder Builder
		{
			get;
			private set;
		}

		/// <summary>
		/// Renders the menu.
		/// </summary>
		/// <returns></returns>
		public string Render()
		{
			var builder = new StringBuilder();

			using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
			{
				using (var html = new XhtmlTextWriter(writer))
				{
					html.RenderBeginTag(HtmlTextWriterTag.Ul);
					html.Indent++;

					foreach (var item in this.Builder.Items)
					{
						var route = RouteTable.Routes[item.Route];

						html.WriteLine();
						html.Indent++;

						var conditions = new RouteValueDictionary(item.Conditions);

						if (conditions.Count > 0)
						{
							var selected = true;

							foreach (var condition in conditions)
							{
								if (!Context.RouteData.Values.ContainsKey(condition.Key))
								{
									continue;
								}

								var value = Context.RouteData.Values[condition.Key] as string;
								var compare = condition.Value as string;

								if (value == null || compare == null)
								{
									continue;
								}

								if (compare.Contains(","))
								{
									var items = compare.Split(',');

									selected = false;

									for (var i = 0; i < items.Length; i++)
									{
										if (value.Equals(items[i]))
										{
											selected = true;
										}
									}
								}
								else
								{
									if (!value.Equals(compare))
									{
										selected = false;
									}
								}
							}

							if (selected)
							{
								html.AddAttribute(HtmlTextWriterAttribute.Class, "selected");
							}
						}

						html.RenderBeginTag(HtmlTextWriterTag.Li);

						var url = route.GetVirtualPath(this.Context, new RouteValueDictionary(item.Parameters)).VirtualPath;

						html.AddAttribute(HtmlTextWriterAttribute.Href, url.AsRoot());

						html.RenderBeginTag(HtmlTextWriterTag.A);
						html.Write(item.Text);
						html.RenderEndTag();

						html.RenderEndTag();
						html.Indent--;
					}

					html.Indent--;
					html.RenderEndTag();
				}
			}

			return builder.ToString();
		}
	}
}